// 导入 NiFi FlowFile 相关的 Java 类
var FlowFile = Java.type('org.apache.nifi.flowfile.FlowFile');
// 导入 Java 8 的日期时间类库，用于精确的日期计算
var LocalDate = Java.type('java.time.LocalDate');
var DateTimeFormatter = Java.type('java.time.format.DateTimeFormatter');

// NiFi 会自动为我们注入 session 和 log 对象
// var session = ...;
// var log = ...;

// 创建一个新的FlowFile，因为这个处理器是数据流的起点
var flowFile = session.create();
if (flowFile != null) {
    try {
        //定义日期时间格式化器
        var formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss');

        //获取昨天的日期
        var yesterday = LocalDate.now().minusDays(1);

        //计算昨天的开始时间 (00:00:00)
        var auditTimeBegin = yesterday.atStartOfDay().format(formatter);

        //计算昨天的结束时间 (23:59:59)
        var endOfYesterday = LocalDate.now().atStartOfDay().minusSeconds(1);
        var auditTimeEnd = endOfYesterday.format(formatter);

        log.info('成功计算出时间范围: {} 到 {}', [auditTimeBegin, auditTimeEnd]);

        //构建JSON对象
        // var timeRangeObject = {
        //     auditTimeBegin: auditTimeBegin,
        //     auditTimeEnd: auditTimeEnd
        // };

        var timeRangeObject = {
            "pageIndex": "1",
            "pageSize": "200",
            "gmtCreatedEnd": "",
            "auditTimeEnd": auditTimeEnd,
            "gmtCreatedBegin": "",
            "hasQueryHistory": "",
            "gmtModifiedBegin": "",
            "tradeNo": "",
            "gmtModifiedEnd": "",
            "cols": "[\"settleId\",\"settleNo\",\"tradeNo\",\"sourceTradeNo\",\"settleInvoiceStatus\",\"tradeSettleOrder.settleId\",\n\"tradeSettleOrder.shopId\",\n\"tradeSettleOrder.auditor\",\n\"tradeSettleOrder.flagIds\",\n\"tradeSettleOrder.payment\",\n\"tradeSettleOrder.departId\",\n\"tradeSettleOrder.settleNo\",\n\"tradeSettleOrder.shopName\",\n\"tradeSettleOrder.auditTime\",\n\"tradeSettleOrder.auditorId\",\n\"tradeSettleOrder.companyId\",\n\"tradeSettleOrder.settleFee\",\n\"tradeSettleOrder.settlerId\",\n\"tradeSettleOrder.chargeType\",\n\"tradeSettleOrder.departName\",\n\"tradeSettleOrder.settleMemo\",\n\"tradeSettleOrder.settleTime\",\n\"tradeSettleOrder.sysFlagIds\",\n\"tradeSettleOrder.accountName\",\n\"tradeSettleOrder.companyName\",\n\"tradeSettleOrder.settleStatus\",\n\"tradeSettleOrder.chargeAccount\",\n\"tradeSettleOrder.onlinePayTime\",\n\"tradeSettleOrder.chargeCurrency\",\n\"tradeSettleOrder.chargeCurrencyCode\",\n\"tradeSettleOrder.chargeExchangeRate\",\n\"tradeSettleOrder.settler\",\n\"tradeSettleOrder.couponFee\",\n\"tradeSettleOrder.differenceFee\",\n\"tradeSettleOrder.settleTotalFee\",\n\"tradeSettleOrder.settleDiscountFee\",\n\"tradeSettleOrderDetailArr.subTradeId31177\",\n\"tradeSettleOrderDetailArr.tradeFrom\",\n\"tradeSettleOrderDetailArr.cost\",\n\"tradeSettleOrderDetailArr.unit\",\n\"tradeSettleOrderDetailArr.payNo\",\n\"tradeSettleOrderDetailArr.cateId\",\n\"tradeSettleOrderDetailArr.remark\",\n\"tradeSettleOrderDetailArr.seller\",\n\"tradeSettleOrderDetailArr.specId\",\n\"tradeSettleOrderDetailArr.taxFee\",\n\"tradeSettleOrderDetailArr.brandId\",\n\"tradeSettleOrderDetailArr.goodsId\",\n\"tradeSettleOrderDetailArr.goodsNo\",\n\"tradeSettleOrderDetailArr.orderNo\",\n\"tradeSettleOrderDetailArr.payTime\",\n\"tradeSettleOrderDetailArr.payType\",\n\"tradeSettleOrderDetailArr.taxRate\",\n\"tradeSettleOrderDetailArr.cateName\",\n\"tradeSettleOrderDetailArr.sellerId\",\n\"tradeSettleOrderDetailArr.settleId\",\n\"tradeSettleOrderDetailArr.specName\",\n\"tradeSettleOrderDetailArr.auditTime\",\n\"tradeSettleOrderDetailArr.brandName\",\n\"tradeSettleOrderDetailArr.goodsName\",\n\"tradeSettleOrderDetailArr.goodsTags\",\n\"tradeSettleOrderDetailArr.sellCount\",\n\"tradeSettleOrderDetailArr.sellPrice\",\n\"tradeSettleOrderDetailArr.sellTotal\",\n\"tradeSettleOrderDetailArr.tradeTime\",\n\"tradeSettleOrderDetailArr.tradeType\",\n\"tradeSettleOrderDetailArr.arriveTime\",\n\"tradeSettleOrderDetailArr.chargeType\",\n\"tradeSettleOrderDetailArr.goodsAlias\",\n\"tradeSettleOrderDetailArr.logisticId\",\n\"tradeSettleOrderDetailArr.mainPostid\",\n\"tradeSettleOrderDetailArr.payAccount\",\n\"tradeSettleOrderDetailArr.payDueDate\",\n\"tradeSettleOrderDetailArr.stockoutNo\",\n\"tradeSettleOrderDetailArr.accountName\",\n\"tradeSettleOrderDetailArr.consignTime\",\n\"tradeSettleOrderDetailArr.discountFee\",\n\"tradeSettleOrderDetailArr.tradeStatus\",\n\"tradeSettleOrderDetailArr.warehouseId\",\n\"tradeSettleOrderDetailArr.lastShipTime\",\n\"tradeSettleOrderDetailArr.logisticName\",\n\"tradeSettleOrderDetailArr.logisticType\",\n\"tradeSettleOrderDetailArr.chargeAccount\",\n\"tradeSettleOrderDetailArr.sourceTradeNo\",\n\"tradeSettleOrderDetailArr.warehouseName\",\n\"tradeSettleOrderDetailArr.goodsAttribute\",\n\"tradeSettleOrderDetailArr.shareFavourableAfterFee\",\n\"tradeSettleOrderDetailArr.tradeNo\",\n\"tradeSettleOrderDetailArr.innerSettleCost\",\n\"tradeSettleOrderDetailArr.shareDiscountFee\",\n\"tradeSettleOrderDetailArr.shareOtherPayment\",\n\"tradeSettleOrderDetailArr.goodsReceiptAmount\",\n\"tradeSettleOrderDetailArr.shareFairSellTotal\",\n\"tradeSettleOrderDetailArr.shareFavourableFee\",\n\"tradeSettleOrderDetailArr.otherShareFavourableFee\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn1\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn2\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn3\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn4\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn5\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn6\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn7\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn8\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn9\",\n\"tradeSettleOrderDetailArr.customizeGoodsColumn10\",\n\"tradeSettleOrderCustomer.settleUnitId\",\n\"tradeSettleOrderCustomer.qq\",\n\"tradeSettleOrderCustomer.email\",\n\"tradeSettleOrderCustomer.weiXin\",\n\"tradeSettleOrderCustomer.wangWang\",\n\"tradeSettleOrderCustomer.customerId\",\n\"tradeSettleOrderCustomer.settleUnit\",\n\"tradeSettleOrderCustomer.customerName\",\n\"tradeSettleOrderCustomer.customerTags\",\n\"tradeSettleOrderCustomer.customerType\",\n\"tradeSettleOrderCustomer.customerGrade\",\n\"tradeSettleOrderCustomer.customerAccount\",\n\"tradeSettleOrderCustomer.customerTypeName\",\n\"tradeSettleOrderCustomer.customerGradeName\",\n\"tradeSettleOrderCustomer.endCustomerAccount\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn1\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn2\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn3\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn4\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn5\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn6\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn7\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn8\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn9\",\n\"tradeSettleOrderColumnExt.customizeTradeColumn1\"]",
            "billDateBegin": "",
            "settleStatus": "",
            "auditTimeBegin": auditTimeBegin,
            "settleNo": "",
            "shopId": "",
            "billDateEnd": "",
            "detailCols": "[\"goodsId\",\"goodsNo\",\"goodsName\",\"unit\"]"
        };
        var jsonPayload = JSON.stringify(timeRangeObject);

        //将JSON字符串写入FlowFile的内容
        // flowFile = session.write(flowFile, function (outputStream) {
        //     outputStream.write(jsonPayload.getBytes('UTF-8'));
        // });
        //引入需要用到的Java接口
        var OutputStreamCallback = Java.type('org.apache.nifi.processor.io.OutputStreamCallback');
        //在调用session.write时，显式地创建一个OutputStreamCallback的实例
        flowFile = session.write(flowFile, new OutputStreamCallback(function (outputStream) {
            outputStream.write(jsonPayload.getBytes('UTF-8'));
        }));

        //为FlowFile添加一个有意义的文件名属性
        flowFile = session.putAttribute(flowFile, 'filename', 'timerange-' + yesterday.toString() + '.json');

        //将新创建并写入内容的FlowFile路由到'success'关系
        session.transfer(flowFile, REL_SUCCESS);

    } catch (e) {
        // 如果发生任何错误，记录日志并将原始FlowFile路由到'failure'
        log.error('生成时间范围JSON时出错', e);
        session.transfer(flowFile, REL_FAILURE);
    }
}